﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class BirdMoveReact : MonoBehaviour {

	private BirdMoveController BMC;

	[System.Serializable]
	public class FloatEvent : UnityEvent<float> { }

	[SerializeField] private Transform boy;
	[SerializeField] private float m_MoveRadius;
	[SerializeField] private FloatEvent boyFlip;
	[SerializeField] private UnityEvent birdLostEvent;


	private void Awake() {
		BMC = GetComponent<BirdMoveController>();
		if (boyFlip == null)
			boyFlip = new FloatEvent();
		if (birdLostEvent == null)
			birdLostEvent = new UnityEvent();
	}

	private void Update() {

		if (!BirdState.Controllable(BMC.state))
			return;
		BoyFaceToBird();

		ToAwayFromBoy();
	}

	void BoyFaceToBird() {

		if (transform.position.x >= boy.position.x) {
			boyFlip.Invoke(1);
		} else {
			boyFlip.Invoke(-1);
		}
	}

	void ToAwayFromBoy() {

		if (Vector2.Distance(transform.position, boy.position) > m_MoveRadius) {
			birdLostEvent.Invoke();
			BMC.state = BirdState.State.Wait;
		}


	}

	private void OnDrawGizmos() {

		Gizmos.color = Color.green;
		Gizmos.DrawWireSphere(boy.position, m_MoveRadius);

	}

}
